package com.eco.fanliapp.common;

import com.eco.fanliapp.entity.TaobaoScheme;
import com.eco.fanliapp.service.ITaobaoSchemeService;
import com.eco.fanliapp.service.RedisTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 联盟信息获取
 */
@Component
@Slf4j
public class OrderTaskUtils {


    @Autowired
    private ITaobaoSchemeService taobaoSchemeService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RedisTemplateService redisTemplateService;

    /**
     * 获取未失效的淘宝联盟TaobaoScheme及appSession
     * @return
     */
    public List<TaobaoScheme> getUsedTaobaoSchemeList(){

        String redisKey = "order_task_taobaoscheme_list";
        List<TaobaoScheme> taobaoSchemes = redisTemplateService.getTByListKey(redisKey, TaobaoScheme.class);
        if (taobaoSchemes == null || taobaoSchemes.size() < 0) {
            taobaoSchemes = taobaoSchemeService.selectDistinctTaobaoSchemeList(null);

            List<TaobaoScheme> taobaoSchemeList = new ArrayList<>();
            //判断appSession是否失效
            for (TaobaoScheme taobaoScheme : taobaoSchemes) {
                try {
                    String appSession = (String) redisTemplate.opsForValue().get("fail_session_" + taobaoScheme.getAppSession());
                    if (appSession != null && !"".equals(appSession) && appSession.length() > 0) {
                        log.info("获取失效的appSession{}", appSession);
                    } else {
                        taobaoSchemeList.add(taobaoScheme);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            if (taobaoSchemeList != null) {
                redisTemplateService.set(redisKey, taobaoSchemeList, 60 * 5);
            }
            return taobaoSchemeList;
        }


        return taobaoSchemes;
    }
    /**
     * 获取其他联盟订单用
     * @return
     */
    public List<TaobaoScheme> getUsedTaobaoScheme(){
        List<TaobaoScheme> taobaoSchemes = taobaoSchemeService.selectDistinctTaobaoScheme(null);
//        String redisKey = "order_task_taobaoscheme_list_integral";
//        List<TaobaoScheme> taobaoSchemes = redisTemplateService.getTByListKey(redisKey, TaobaoScheme.class);
//        if (taobaoSchemes == null || taobaoSchemes.size() < 0) {
//            taobaoSchemes = taobaoSchemeService.selectDistinctTaobaoScheme(null);
//
//            List<TaobaoScheme> taobaoSchemeList = new ArrayList<>();
//            //判断appSession是否失效
//            for (TaobaoScheme taobaoScheme : taobaoSchemes) {
//                try {
//                    String appSession = (String) redisTemplate.opsForValue().get("fail_session_" + taobaoScheme.getAppSession());
//                    if (appSession != null && !"".equals(appSession) && appSession.length() > 0) {
//                        log.info("获取失效的appSession{}", appSession);
//                    } else {
//                        taobaoSchemeList.add(taobaoScheme);
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//
//            if (taobaoSchemeList != null) {
//                redisTemplateService.set(redisKey, taobaoSchemeList, 60 * 5);
//            }
//            return taobaoSchemes;
//        }
        return taobaoSchemes;
    }

    /**
     * TODO 获取当前时间的20分钟前，用来订单查询
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getSystemTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.MINUTE, -120);//获取20分钟前的时间
        Date beforeD = beforeTime.getTime();
        return df.format(beforeD);
    }

    /**
     * TODO 获取当前时间的2小时前，用来订单查询
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getTwoHourSystemTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.MINUTE, -140);//获取2小时前的时间
        Date beforeD = beforeTime.getTime();
        return df.format(beforeD);
    }
    /**
     * TODO 获取当前时间的20分钟前，用来订单查询
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getNowTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(new Date());
    }
}
